<template>
	<view>
		<u-navbar title="群名称">
			<view v-if="canEdit" slot="right" class="u-p-x-16 u-p-y-4 u-m-r-30 radius" @tap="save" :class="{'bg-grey':!name,'bg-blue':name}">
				<text class="u-font-28">保存</text>
			</view>
		</u-navbar>
		<view class="u-m-t-20 bg-white u-p-30">
			<input class="u-font-28" maxlength="20" v-model="name" placeholder="请输入昵称" :disabled="!canEdit"/>
			<view class="text-gray u-text-right u-m-t-40">
				<text>{{name.length}}/20</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {goback} from '@/utils/util.js'
	import {state} from "../bus.js"
	import {editGroupName} from "@/api/addr.js"
	import {mapGetters,mapActions} from "vuex"
	export default {
		data() {
			return {
				name:''
			};
		},
		computed:{
			...mapGetters('user',['userId']),
			...mapGetters('chat',['conversations']),
			conversation(){
				return this.conversations.find(({id})=>id==state.info.id)
			},
			canEdit(){
				return state.power.vest=='群主'||state.power.vest=='管理员'
			}
		},
		onLoad(){
			this.name = state.info.name
		},
		methods:{
			...mapActions('chat',['setConversation']),
			async save(){
				if(this.save.lock||!this.name){
					return
				}
				try{
					this.save.lock = true
					await editGroupName({id:state.info.id,name:this.name})
					state.info.name = this.name
					this.setConversation({
						...this.conversation,
						name:this.name
					})
					goback()
				}finally{
					this.save.lock = false
				}
			}
		}
	}
</script>

<style lang="scss">

</style>
